package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.behavior.entity.ApLikesBehavior;
import com.usian.behavior.mapper.ApLikesBehaviorMapper;
import com.usian.behavior.service.IApLikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.threadlocal.UserThreadLocalUtils;
import com.usian.model.behavior.dtos.LikesBehaviorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-07-18
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements IApLikesBehaviorService {
    /**
     * 储存点赞数据
     * @param dto
     */
    @Override
    public void like(LikesBehaviorDto dto) {
        //定义一个初始化boolean
        boolean isUser=false;
        //获取线程中的userId
        Integer userId = UserThreadLocalUtils.getUserId();
        //获取机械码
        String equipmentId = dto.getEquipmentId();
        //判断用户和机械码不能同时为空
        if (userId==null&&equipmentId==null){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"用户和机械码不能同时为空");
        }
        if(userId != null && userId != 0){
            isUser = true;
        }
        //查询数据库中是否有相关用户或机械码的数据
        LambdaQueryWrapper<ApLikesBehavior> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApLikesBehavior::getArticleId,dto.getArticleId());
        if (isUser){
            lambdaQueryWrapper.eq(ApLikesBehavior::getApUserId,userId);
        }else {
            lambdaQueryWrapper.eq(ApLikesBehavior::getEquipmentId,equipmentId);
        }
        ApLikesBehavior apLikesBehavior = getOne(lambdaQueryWrapper);
        //如果apLikesBehavior==null说明数据库中没有数据要去新增
        if (apLikesBehavior==null){
            //没有行为需要新增
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setArticleId(dto.getArticleId());
            if (isUser){
                apLikesBehavior.setApUserId(userId);
            }else {
                apLikesBehavior.setEquipmentId(equipmentId);
            }
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setCreatedTime(new Date());
            save(apLikesBehavior);
        }else {
            //进行修改
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setUpdateTime(new Date());
            updateById(apLikesBehavior);
        }


    }

    /**
     * 根据id查询点赞行为
     * @param userId
     * @param equipmentId
     * @param articleId
     * @param type
     * @return
     */
    @Override
    public ApLikesBehavior findLikeByArticleId(Integer userId, String equipmentId, Long articleId, Integer type) {
        if (userId==null && equipmentId==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"用户id和机械码不能同时为空");
        }

        boolean isUser = false;

        if (userId!=null && userId!=0){
            isUser=true;
        }
        LambdaQueryWrapper<ApLikesBehavior> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApLikesBehavior::getArticleId,articleId);
        lambdaQueryWrapper.eq(ApLikesBehavior::getType,type);

        if (isUser){
            lambdaQueryWrapper.eq(ApLikesBehavior::getApUserId,userId);
        }else {
            lambdaQueryWrapper.eq(ApLikesBehavior::getEquipmentId,equipmentId);
        }

        ApLikesBehavior apLikesBehavior = getOne(lambdaQueryWrapper);

        return apLikesBehavior;
    }
}
